package cn.stylefeng.guns.modular.wordstudy.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.wordstudy.entity.StudyHistory;
import cn.stylefeng.guns.modular.wordstudy.mapper.StudyHistoryMapper;
import cn.stylefeng.guns.modular.wordstudy.model.params.StudyHistoryParam;
import cn.stylefeng.guns.modular.wordstudy.model.result.StudyHistoryResult;
import cn.stylefeng.guns.modular.wordstudy.service.StudyHistoryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 学习历史 服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-28
 */
@Service
public class StudyHistoryServiceImpl extends ServiceImpl<StudyHistoryMapper, StudyHistory> implements StudyHistoryService {

    @Override
    public void add(StudyHistoryParam param) {
        StudyHistory entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StudyHistoryParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(StudyHistoryParam param) {
        StudyHistory oldEntity = getOldEntity(param);
        StudyHistory newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StudyHistoryResult findBySpec(StudyHistoryParam param) {
        return null;
    }

    @Override
    public StudyHistoryResult studyData(StudyHistoryParam param) {
        StudyHistoryResult studyHistoryResult = new StudyHistoryResult();
        // 学习数据
        String today = DateUtils.formateYYYYMMDD(DateUtils.getSystemDate());
        int total = this.count(new QueryWrapper<StudyHistory>().eq("open_id", param.getOpenId()).eq("date_format(create_time,'%Y-%m-%d')", today));
        studyHistoryResult.setTotal(total);
        int newWord = this.count(new QueryWrapper<StudyHistory>().eq("open_id", param.getOpenId()).eq("date_format(create_time,'%Y-%m-%d')", today).eq("review_flag", "2"));
        studyHistoryResult.setNewWord(newWord);
        studyHistoryResult.setOldWord(total - newWord);
        return studyHistoryResult;
    }

    @Override
    public List<StudyHistoryResult> findListBySpec(StudyHistoryParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StudyHistoryParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StudyHistoryParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StudyHistory getOldEntity(StudyHistoryParam param) {
        return this.getById(getKey(param));
    }

    private StudyHistory getEntity(StudyHistoryParam param) {
        StudyHistory entity = new StudyHistory();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
